package com.tpc.control.bean;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import javax.faces.event.ActionEvent;

import com.tpc.control.jpa.EntityManagerHelper;
import com.tpc.control.jpa.ReserveHeader;
import com.tpc.control.jpa.ReserveHeaderDAO;
import com.tpc.control.jpa.ReserveItem;
import com.tpc.control.jpa.ReserveItemDAO;
import com.tpc.control.util.Connector;
import com.tpc.control.util.ReportEntity;
import com.tpc.control.util.Util;
import com.tpc.control.util.WriteExcel;

import jxl.write.WriteException;




/**
 * 
 */

/**
 * @author tpcorp
 *
 */


public class Con120 {
	
	private List items;
	
	ReserveItemDAO dao = new ReserveItemDAO();

	private String start="";
	private String stop="";
	private String reserve_no;
	private String reportString = "";
	
	private int currentIndex = 0;
	private String currentReserveNo="";
	private List<ReportEntity> reportLists = new ArrayList<ReportEntity>();
	private List<ReportEntity> selectedReportLists = new ArrayList<ReportEntity>();

	private List<List<ReserveItem>> allDataList = new ArrayList<List<ReserveItem>>();
	private List<String> reserveNoLists = new ArrayList<String>();


	public void generateReport(ActionEvent e) throws WriteException, IOException
	{
		WriteExcel.generateReport(reportLists,reserveNoLists);
	}
	
	public String gotoCon120()
	{
		this.start="";
		this.stop="";
		search();
		return "con120";
	}
	public void next(ActionEvent e)
	{
		if(currentIndex < reserveNoLists.size()-1)
		{
			
			currentIndex+=1;
			
		}

		rebind();
	}
	public void previous(ActionEvent e)
	{
		if(currentIndex > 0)
		{
			currentIndex-=1;
		}
		rebind();
	}
	public void first(ActionEvent e)
	{
		currentIndex=0;
		rebind();
	}
	public void last(ActionEvent e)
	{
		currentIndex=reserveNoLists.size()-1;
		rebind();
	}
	
	public void search(ActionEvent e)
	{
		search();
	}
	public void search()
	{
		this.currentIndex =0;
		allDataList.clear();
		reserveNoLists.clear();
		reportLists.clear();
		selectedReportLists.clear();

		ReserveHeaderDAO headerDAO = new ReserveHeaderDAO();
	
		List<String> headerList = Connector.getReserveHeader(start.toUpperCase(), stop.toUpperCase());
		
		for(int i=0;i<headerList.size();i++)
		{
			//EntityManagerHelper.closeEntityManager();
			//EntityManagerHelper.beginTransaction();
			ReserveHeader reserveHeader = headerDAO.findById(headerList.get(i));
			
			Set<ReserveItem> itemSet = reserveHeader.getReserveItems();
			//EntityManagerHelper.commit();
			//EntityManagerHelper.closeEntityManager();
			
			
			List<ReserveItem> itemList = new ArrayList<ReserveItem>(itemSet);
			List<ReserveItem> sortedList = new ArrayList<ReserveItem>();
			
			int j=1;
			
			while(itemList.size()>0)
			{
				for(int k=0;k<itemList.size();k++)
				{
					if(itemList.get(k).getId().getReserveLine().equals(j))
					{
						sortedList.add(itemList.get(k));
						itemList.remove(k);
					}
				}
				j++;
			}
			allDataList.add(sortedList);
		}
		
		
		for(int i=0;i<allDataList.size();i++)
		{
			List<ReserveItem> selectedItems = allDataList.get(i);
		
			reserveNoLists.add(selectedItems.get(0).getId().getReserveNo());
			
		for(int j=0; j<selectedItems.size();j++)
		{
			ReserveItem item = selectedItems.get(j);
			//�ӴѺ���
			String line = String.valueOf(item.getId().getReserveLine());
			//���� com
			String materialNo = item.getMaterialNo();
			//���� design
			String materialDesign = item.getMaterialDesign();
			//Ἱ�
			String division = item.getDivision();
			//����ҳ���ͧ
			//NumberFormat formatter = new DecimalFormat("#0.00");
			double d = item.getReserveQty();
		    DecimalFormat df = new DecimalFormat("###,##0.00");
		    //System.out.println(df.format(d));

			//String qty = item.getReserveQty().toString("#,##0.00");
			String qty = df.format(d);
			//˹���
			String uom = item.getUom();
			//��˹���
			String Date = String.valueOf(item.getDeliveryDate().getDate());
			if(Date.length()<2)
			{
				Date = "0" + Date;
			}
			String Month = String.valueOf(item.getDeliveryDate().getMonth() + 1);
			if(Month.length()<2)
			{
				Month = "0" + Month;
			}
			String Year = String.valueOf(item.getDeliveryDate().getYear() + 1900);
			
			String deliveryDate = Date + "/" + Month + "/" + Year;
			//ʶҹ�㺨ͧ
			String status = Util.getStatus(item.getStatus());

			
			String reserveNo = String.valueOf(item.getId().getReserveNo().toUpperCase());
			
			this.reportString = reportString + line + " " + materialNo + "" + materialDesign + " " + division + " " + qty + " " + uom + " " + deliveryDate + " " + status + "\r";
			
			ReportEntity entity = new ReportEntity();
			
			entity.setReserveNo(reserveNo);
			entity.setLine(line);
			entity.setDivision(division);
			entity.setDeliveryDate(deliveryDate);
			entity.setMaterialDesign(materialDesign);
			entity.setMaterialNo(materialNo);
			entity.setQty(qty);
			entity.setStatus(status);
			entity.setUom(uom);
			
			reportLists.add(entity);
			
		}
		}

		selectedReportLists.clear();
		rebind();
	}
	public  void rebind()
	{
		selectedReportLists.clear();
		for(int i=0;i<reportLists.size();i++)
		{
			ReportEntity entity = reportLists.get(i);
			System.out.println(reserveNoLists.get(currentIndex));
			if(entity.getReserveNo().equals(reserveNoLists.get(currentIndex)))
			{
				selectedReportLists.add(entity);
			}
		}
	}
	


	public void setReportString(String reportString) {
		this.reportString = reportString;
	}



	public String getReportString() {
		return reportString;
	}

	public void setReportLists(List<ReportEntity> reportLists) {
		this.reportLists = reportLists;
	}
	public List<ReportEntity> getReportLists() {
		return reportLists;
	}
	public void setSelectedReportLists(List<ReportEntity> selectedReportLists) {
		this.selectedReportLists = selectedReportLists;
	}
	public List<ReportEntity> getSelectedReportLists() {
		return selectedReportLists;
	}
	public void setCurrentReserveNo(String currentReserveNo) {
		
		this.currentReserveNo = currentReserveNo;
	}
	public String getCurrentReserveNo() {
		try
		{
		return this.reserveNoLists.get(currentIndex);
		}
		catch(Exception ex)
		{
			return "";
		}
	}
	public void setStart(String start) {
		this.start = start;
	}
	public String getStart() {
		return start;
	}
	public void setStop(String stop) {
		this.stop = stop;
	}
	public String getStop() {
		return stop;
	}
	public void setReserve_no(String reserve_no) {
		this.reserve_no = reserve_no;
	}
	public String getReserve_no() {
		return reserve_no;
	}

}